package com.yanqu.road.logic.redis;

import org.apache.logging.log4j.LogManager;
import org.apache.logging.log4j.Logger;
import redis.clients.jedis.Tuple;

import java.util.HashSet;
import java.util.Set;

public class RedisUserServerList {

    private static Logger logger = LogManager.getLogger(RedisUserServerList.class.getName());

    public static Set<String> getPlayerServerList(String openId){
        Set<String> userRegisterServerList =
                RedisManger.getRedisUtil().zmember(RedisKeyDefineHelper.getPlayerRegisterServerListKey(openId),0,System.currentTimeMillis()/1000);

        return userRegisterServerList;
    }

    public static Set<Tuple> getPlayerServerListTuple(String openId) {
        Set<Tuple> list = RedisManger.getRedisUtil().zRangeWithScores(RedisKeyDefineHelper.getPlayerRegisterServerListKey(openId), 0, System.currentTimeMillis()/1000);
        if (list == null) {
            return new HashSet<>();
        } else {
            return list;
        }
    }

    public static void addToRegisterServerList(String openId, long serverId){
        try{
            RedisManger.getRedisUtil().zadd(RedisKeyDefineHelper.getPlayerRegisterServerListKey(openId),System.currentTimeMillis()/1000,serverId+"");
            //设置key半年失效
            RedisManger.getRedisUtil().expire(RedisKeyDefineHelper.getPlayerRegisterServerListKey(openId),60*60*24*30*6);
        }catch (Exception e){
            logger.error("addToRegisterServerList error", e);
        }
    }
}
